if (!jsx3.lang.Class.forName("ux.cdb.admin.system.sysuser.SysUserPage")) {

jsx3.Class.defineClass(
        "ux.cdb.admin.system.sysuser.SysUserPage",
        jsx3.gui.Block,
        null,
        function (objRoot) {
            objRoot.prototype.docDir = "ux/cdb/admin/system/sysuser/docs/";
            objRoot.prototype.namespace = "ux.cdb.admin";

			/**
			 * Helper Functions, return a copy function with 
			 * @param fn {Function}
			 * @param scope {Object} 
			 */
			objRoot.prototype.bind = function(fn, scope) {
				scope = scope || this;
				return function() {
					fn.apply(scope, arguments);
				};
			};
			
			/**
			 * Add event to jsx Object
			 * @param name {String} jsx object name or comma-splitted names
			 * @param event {String} event name
			 * @param fct {Function} event handler
			 */
			objRoot.prototype.on = function (name, event, fct) {
				var names = name.split(","), block=this;
				for(var i=0; i<names.length; i++) {
					block = block.get(names[i]);
				}
				block[event] = this.bind(fct);
			};

			/**
			 * get property
			 * @param name {String} property names 
			 * @param token {Object} token of current property
			 * @private
			 */
			objRoot.prototype.property = function(name, token) {
                return ux.megatron.Main.getServer().getDynamicProperty(name, token);
            };
            
			/**
			 * set states of a list of buttons, enable or disable
			 * @param enableBtns {Array} button names to be enabled
			 * @param disableBtns {Array} button names to be disabled
			 * @private
			 */
            objRoot.prototype._setBtns = function (enableBtns, disableBtns) {
                var i;
                if(enableBtns) {
                    for(i=0; i<enableBtns.length; i++) {
                        this.get(enableBtns[i]).enable();
                    }
                }
                if(disableBtns) {
                    for(i=0; i<disableBtns.length; i++) {
                        this.get(disableBtns[i]).disable();
                    }
                }
            };

			/**
			 * This function will be called after deserialize
			 */
            objRoot.prototype.onAfterDeserialize = function() {
/* @JSC :: begin DEBUG */
				jsx3.log('SysUserPage#onAfterDeserialize : Begin onAfterDeserialize ...');
/* @JSC :: end */
			};

			objRoot.prototype.initialize = function() {
/* @JSC :: begin DEBUG */
				jsx3.log('SysUserPage#initialize : Begin initialize ...');
/* @JSC :: end */
                var me = this;
                this.matrix = this.get("mtxMain");
/* @JSC :: begin DEBUG */
				jsx3.log('SysUserPage#initialize : Begin binding PageWheel ...');
/* @JSC :: end */
				this.pageSize = 15;
				this.totalRecordCount = 0;
				this.currentPage = 1;
				this.totalPages = 1; 
				this.pageLoaded = false;
				
                this.pagebar = this.get("blkPageWheel");
/* @JSC :: begin DEBUG */
				jsx3.log('SysUserPage#initialize : Begin binding matrix events ...');
/* @JSC :: end */
                this.matrix.subscribe(ux.cdb.admin.gui.Matrix.EVENT_CHECKALL, me, me.onCheckMatrix);

                this.on("mtxMain,mcCheckbox,checkbox", "onToggle", me.onCheckMatrix);
/* @JSC :: begin DEBUG */
                jsx3.log('SysUserPage#initialize : Begin binding click event ...');
/* @JSC :: end */
                this.on("btnQuery", "_query", me.doRefresh);
                this.on("btnUnlock", "onClick", me.onUnlock);

                this.on("btnRefresh", "onClick", me.doRefresh);

				this.get('btnUnlock').disable();
				//this.doRefresh();
            };

            objRoot.prototype.setCommonCache = function() {
				var me = this;
				/* ux.cdb.admin.CommonCache.loadCache(['INTERNAL_ORG'],me, */
				(function(){
					var _cache = ux.cdb.admin.CommonCache.getInstance();
					me.get('fieldOfInternalOrgId').getChild(1).setSourceXML(_cache.getCache('CommonCache_INTERNAL_ORG'));
					me.get('fieldOfInternalOrgId').getChild(1).repaint();
				})();
			};
			
			/**
			 * Handler for unlock sysuser button
			 */
            objRoot.prototype.onUnlock = function() {
                var me = this;
                var tipTitle = this.property("@cdb@system@sysuser@DlgConfirmTitle");
                var tipContent = this.property("@cdb@system@sysuser@DlgConfirmContent");
                ux.megatron.Dialog.confirm(tipTitle, tipContent, function(dialog){
					try{
	                    var nodes = me.matrix.getCheckedNodes();
						me._unlockSysUser(nodes.get(0).getAttribute('jsxid'), dialog);
						//dialog.setCursor('wait'); //here is a bug.
						var btns = dialog.getDescendantsOfType('ux.megatron.gui.Button');
						jsx3.log('onUnlock#btns : ' + btns);
						for(var i=0,btn; btn=btns[i]; i++){
							btn.setCursor('wait');
							btn.disable();
						}
					}catch(e){
						alert('there is a error occured[' + e.message + ']!');
						dialog.doClose();
					}
                }, null);
            };
			
			/*
			* @private
			*/
            objRoot.prototype._unlockSysUser = function(userId, dialog) {
				ux.megatron.util.Dwr.callDwr("ManagerUserUCC", "removeUserByUserLogname", [userId], this,
					function(data){
/* @JSC :: begin DEBUG */
						jsx3.log('ManagerUserUCC#removeUserByUserLogname['+userId+'] data : '+dwr.util.toDescriptiveString(data, 3));
/* @JSC :: end */
						if (data != null && typeof data == 'object') {
							this.doRefresh();
		                    this.onCheckMatrix();
		                    dialog.doClose();
			                var tipTitle = this.property("@cdb@system@sysuser@DlgAlertTitle");
			                var tipContent = (data.res.flag == 1 ? 
								this.property("@cdb@system@sysuser@DlgAlertContentSuccess") :
								this.property("@cdb@system@sysuser@DlgAlertContentFailed")) + 
								'<br/>[' + data.res.msg + ']';
							
							ux.megatron.Dialog.alert(tipTitle, data.res.msg, function(_dialog){
								_dialog.doClose();
							});
						}
					});
			};
            
			/**
			 * Handler for check some records in Matrix mtxMain
			 */
            objRoot.prototype.onCheckMatrix = function() {
                var nodes = this.matrix.getCheckedNodes();
                if(nodes.size() === 0) {
                    this._setBtns(null, ["btnUnlock"]);
                } else if(nodes.size() == 1) {
                    this._setBtns(["btnUnlock"]);
					this.selectedNode = nodes.get(0);
                } else {//Muilt select
					this._setBtns(null, ["btnUnlock"]);
                }
            };
			
			objRoot.prototype.doRefresh = function() {
				this.currentPage = 1;
				this._loadSysUserList();
			};

			/**
			* Load sysuser data
			* @private
			*/
            objRoot.prototype._loadSysUserList = function() {
/* @JSC :: begin DEBUG */
				jsx3.log('ManagerUserUCC#_loadSysUserList begin...');
/* @JSC :: end */
				var _data = this.get('paneQueryDetail').getDataAsObject();
				var params = [
					_data.logonName || null,
					_data.internalOrgId || null
				];
/* @JSC :: begin DEBUG */
				jsx3.log('ManagerUserUCC#_loadSysUserList params:' + params);
/* @JSC :: end */
				ux.megatron.util.Dwr.callDwr("ManagerUserUCC", "getAllOnlineUser", params, this,
					function(data){
						if (data != null && typeof data == 'object') {
							this._setSysUserList(data);
/* @JSC :: begin DEBUG */
							jsx3.log('ManagerUserUCC#_loadSysUserList data : '+dwr.util.toDescriptiveString(data, 3));
/* @JSC :: end */
						}
					});
            };
			
			/**
			* set sysuser list into matrix mtxMain
			* @private
			*/
			objRoot.prototype._setSysUserList = function(data) {
				if(data && data instanceof Object) {
					var tmpCDF = jsx3.xml.CDF.Document.newDocument();
					//this.loadPageControler(data.content.page.count); 
					var records = data.content.data;
/* @JSC :: begin DEBUG */
					jsx3.log("SysUserPage#setSysUserList : records : " + dwr.util.toDescriptiveString(records, 3));
/* @JSC :: end */
					for(var i=0,record;record=records[i];i++){
						var jsxid = record.sysUserId ? record.sysUserId : i;
						tmpCDF.insertRecord(record, null, false).setAttribute('jsxid', jsxid);
					}
/* @JSC :: begin DEBUG */
					jsx3.log("SysUserPage#setSysUserList : CDF : " + tmpCDF);
/* @JSC :: end */
					var _cache = ux.megatron.Main.getServer().getCache();
					_cache.clearById('SysUserMatrix_0302');
					_cache.setDocument('SysUserMatrix_0302', tmpCDF);
	                this.matrix.repaintData();
	                this.onCheckMatrix();			
				} else {
					jsx3.log('SysUserPage#setSysUserList cdf data of matrix:' + data);
				}
			};
			
			objRoot.prototype.loadPageControler = function(count) {
				this.totalRecordCount = count;
				this.totalPages = (count%this.pageSize>0) ? (parseInt(count/this.pageSize, 10) + 1) : (count/this.pageSize);
				if(!this.pageLoaded){
					var me = this;
					this.pagebar.initialize();
					this.pagebar.bindWithMatrix(me,me.onPageChange,me.matrix);
					this.pageLoaded = true;
				}
				this.pagebar.setValue(this.currentPage, this.totalPages, this.pageSize, this.totalRecordCount);
			};

			objRoot.prototype.onPageChange = function(msg) {
				this.currentPage = msg.currentPageNumber;
				this._loadSysUserList();
			};

			/**
			 * called when destroy event fired
			 */
            objRoot.prototype.onDestroy = function() {
				jsx3.log('SysUserPage#onDestroy.');
            };
        });
}